{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-10-16T08:38:58.432377700Z",
     "start_time": "2024-10-16T08:38:58.320427100Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两代纳税\n",
      "我今年25\n"
     ]
    }
   ],
   "source": [
    "class ClassA():\n",
    "    var='两代纳税'\n",
    "    \n",
    "    @classmethod\n",
    "    def fun1(cls,age):\n",
    "        #相当于this\n",
    "        print(cls.var)#调用类内数据 要加注解\n",
    "        print(\"我今年\"+str(age))#调用类内数据 要加注解\n",
    "        \n",
    "ClassA.fun1(25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两代纳税\n",
      "我今年25\n",
      "两代纳税\n",
      "我今年25\n",
      "两代纳税\n"
     ]
    }
   ],
   "source": [
    "class ClassAA():\n",
    "    var='两代纳税'\n",
    "\n",
    "    def fun1(self,age):\n",
    "        #相当于this\n",
    "        print(self.var)#调用类内数据 要加注解\n",
    "        print(\"我今年\"+str(age))#调用类内数据 要加注解\n",
    "        \n",
    "ClassA.fun1(25)\n",
    "print()\n",
    "a=ClassAA()\n",
    "a.fun1(25)\n",
    "print()\n",
    "print(a.var)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-16T08:44:01.046889100Z",
     "start_time": "2024-10-16T08:44:01.002613400Z"
    }
   },
   "id": "74a287691f66092b"
  },
  {
   "cell_type": "markdown",
   "source": [
    "* 类属性改变，实例属性改变。\n",
    "* 类方法改变，实例方法改变"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ea1b3d0d8e53029a"
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "ClassB.fun1() missing 1 required positional argument: 'cls'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[3], line 8\u001B[0m\n\u001B[0;32m      4\u001B[0m     \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mfun1\u001B[39m(\u001B[38;5;28mcls\u001B[39m):\n\u001B[0;32m      5\u001B[0m         \u001B[38;5;66;03m#相当于this\u001B[39;00m\n\u001B[0;32m      6\u001B[0m         \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39mvar)\n\u001B[1;32m----> 8\u001B[0m ClassB\u001B[38;5;241m.\u001B[39mfun1()\n",
      "\u001B[1;31mTypeError\u001B[0m: ClassB.fun1() missing 1 required positional argument: 'cls'"
     ]
    }
   ],
   "source": [
    "class ClassB():\n",
    "    var='两代纳税'\n",
    "    \n",
    "    def fun1(cls):\n",
    "        #相当于this\n",
    "        #global nolocal\n",
    "        print(cls.var)\n",
    "        \n",
    "ClassB.fun1()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-16T08:37:07.788906300Z",
     "start_time": "2024-10-16T08:37:07.752607100Z"
    }
   },
   "id": "fe1974e6798a8715"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加我好友V你50\n"
     ]
    }
   ],
   "source": [
    "#类方法修改\n",
    "def newFun1():\n",
    "    print(\"加我好友V你50\")\n",
    "\n",
    "ClassA.fun1=newFun1  #替换方法\n",
    "ClassA.fun1()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-16T08:54:31.195929400Z",
     "start_time": "2024-10-16T08:54:31.176277200Z"
    }
   },
   "id": "16a318c3cab44357"
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实例化成功，你好\n"
     ]
    }
   ],
   "source": [
    "#初始化函数  类似与java static调用类，直接调初始化\n",
    "class Classgg(object):\n",
    "    \n",
    "    def __init__(self,msg):\n",
    "        print('实例化成功，'+msg)\n",
    "A=Classgg('你好')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-16T09:05:34.711782Z",
     "start_time": "2024-10-16T09:05:34.572162100Z"
    }
   },
   "id": "3a176b853104379a"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24\n"
     ]
    }
   ],
   "source": [
    "#阶乘\n",
    "def loggg(n):\n",
    "    if n==0:\n",
    "        return 1\n",
    "    else:\n",
    "        return n*loggg(n-1)\n",
    "\n",
    "print(loggg(4))    "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-16T08:56:26.129389700Z",
     "start_time": "2024-10-16T08:56:26.034444400Z"
    }
   },
   "id": "112c940b36958e09"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#匿名函数\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cab1a3c36bc66f8b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
